package com.yyds.yygh.hospclient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyds.yygh.common.exception.YyghException;
import com.yyds.yygh.common.result.ResultStatus;
import com.yyds.yygh.hospclient.mapper.HospitalSetMapper;
import com.yyds.yygh.hospclient.service.HospitalSetService;
import com.yyds.yygh.model.hosp.HospitalSet;
import com.yyds.yygh.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper,HospitalSet> implements HospitalSetService {
    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public String getSignKey(String hoscode) {
        //根据hoscode查询医院是否存在
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sign_key", "status").eq("hoscode", hoscode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(queryWrapper);
        //判断医院是否存在 状态是否为1
        if (null == hospitalSet){
            throw new YyghException(ResultStatus.HOSPITAL_OPEN);
        }if (hospitalSet.getStatus().intValue() == 0){
            throw new YyghException(ResultStatus.HOSPITAL_LOCK);
        }

        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = hospitalSetMapper.selectOne(queryWrapper);
        if (hospitalSet==null){
            throw new YyghException(ResultStatus.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }
}
